﻿using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Util;
using Lucene.Net.Analysis;
using System;
using System.Collections.Generic;
using System.Text;
using JiebaNet.Segmenter;
using Lucene.Net.Documents;
using System.ComponentModel;
using Lucene.Net.Search;

namespace ServiceCore.Tool
{

    public interface Ilucene_service
    {

    }
    public class lucene_service : Ilucene_service
    {
        public lucene_service()
        {

        }
        private static IndexWriter _lucene_indexWriter = null;
        public static IndexWriter lucene_writer
        {
            get
            {
                if (_lucene_indexWriter == null || _lucene_indexWriter.IsClosed)
                {
                    Analyzer analyzer = new JieBaAnalyzer(TokenizerMode.Search);
                    IndexWriterConfig _indexWriterConfig = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, analyzer);
                    _indexWriterConfig.OpenMode = OpenMode.CREATE_OR_APPEND;
                    _indexWriterConfig.IndexDeletionPolicy = new KeepOnlyLastCommitDeletionPolicy(); 
                    var indexLocation = @"C:\Index";
                    var dir = FSDirectory.Open(indexLocation);
                    IndexWriter _indexWriter = new IndexWriter(dir, _indexWriterConfig);
                    _lucene_indexWriter = _indexWriter;
                    //_lucene_indexWriter.Commit();
                }
                return _lucene_indexWriter;
            }
        }
        public static List<object> CreateIndex()
        {
            var source = new
            {
                Name = "王五",
                FavoritePhrase = "The quick brown fox jumps over the lazy dog"
            };
            Document doc = new Document
            {
                new Lucene.Net.Documents.Int64Field("time",1234567891234, Field.Store.YES),
                // StringField indexes but doesn't tokenize
                new Field("name",source.Name,new FieldType(){
                     IsIndexed = true,
                      IsStored = true,
                       IsTokenized = false,
                }),
                new TextField("favoritePhrase",
                    source.FavoritePhrase,
                    Field.Store.YES),
            };
            lucene_writer.AddDocument(doc, new JieBaAnalyzer(TokenizerMode.Search));
            lucene_writer.Flush(triggerMerge: false, applyAllDeletes: false); 
            lucene_writer.Dispose();
             
            IndexSearcher isearcher = new IndexSearcher(lucene_writer.GetReader(applyAllDeletes: false));
            //Term term = new Term("name", "王五");
            //WildcardQuery wildcardQuery = new WildcardQuery(term);
            //Term term2 = new Term("favoritePhrase", "The quick brown fox jumps over the lazy dog");
            //TermQuery termQuery = new TermQuery(term2);
            //BooleanQuery booleanQuery = new BooleanQuery();
            //booleanQuery.Add(wildcardQuery, Occur.MUST);
            //booleanQuery.Add(termQuery, Occur.MUST);  
            var search_result = isearcher.Search(new TermQuery(new Term("name", "王五")), 10); 
            ScoreDoc[] hits = search_result.ScoreDocs;
            List<object> results = new List<object>();
            foreach (var item in search_result.ScoreDocs)
            {
                Document docs = isearcher.Doc(item.Doc);
                string Name = docs.Get("name");
                string time = docs.Get("time");
                string favoritePhrase = docs.Get("favoritePhrase");
                results.Add(new { Name, time, favoritePhrase });
            }
            lucene_writer.Dispose(); 
            //lucene_writer.DeleteDocuments(new Term("name", "王五"));
            //lucene_writer.Dispose();
            return results;
        }

        public class Rk_Doc
        {
            [Description("StringField")]
            /// <summary>
            /// 文章编号
            /// </summary>
            public string Id { get; set; }
            [Description("TextField")]
            /// <summary>
            /// 文章标题
            /// </summary>
            public string Article_Name { get; set; }
            [Description("StoredField")]
            /// <summary>
            /// 文章内容
            /// </summary>
            public string Article_Content { get; set; }
            [Description("TextField")]
            /// <summary>
            /// 文章检索内容
            /// </summary> 
            public string Article_Content_ForIndex { get; set; }
            [Description("LongField")]
            /// <summary>
            /// 文章状态 0未发布 1已发布
            /// </summary>
            public int Article_State { get; set; }
            [Description("LongField")]
            /// <summary>
            /// 是否私有
            /// </summary>
            public int Is_rk_private { get; set; }
            [Description("LongField")]
            /// <summary>
            /// 作者
            /// </summary>
            public string author { get; set; } 
            [Description("LongField")]
            public long show_time { get; set; }
            [Description("LongField")]
            public long add_time { get; set; }
            [Description("LongField")]
            /// <summary>
            /// 点击率
            /// </summary>
            public int hit { get; set; }
            [Description("StringField")]
            /// <summary>
            /// 文章编号
            /// </summary>
            public string add_uid { get; set; } 
            [Description("StringField")]
            /// <summary>
            /// 类型A
            /// </summary>
            public string type_id_a { get; set; }
            [Description("StringField")]
            /// <summary>
            /// 类型A
            /// </summary>
            public string type_id_b { get; set; }
            [Description("StringField")]
            /// <summary>
            /// 类型A
            /// </summary>
            public string type_id_c { get; set; }
        }
    }
}
